package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima56;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class SolrIndexQueryTest {

    @Test
    public void testQueryIndex() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        //从响应数据中获取结果数据
        SolrDocumentList results = queryResponse.getResults();
        results.forEach(reselt->{
            String id = (String) reselt.get("id");
            String title = (String) reselt.get("title");
            Long price = Long.valueOf(reselt.get("price").toString());
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });

    }

    @Test
    public void testRangeQueryIndex() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象,范围查询，包含左右边界
        SolrQuery solrQuery = new SolrQuery("price:[300000 TO *]");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        //从响应数据中获取结果数据
        SolrDocumentList results = queryResponse.getResults();
        results.forEach(reselt->{
            String id = (String) reselt.get("id");
            String title = (String) reselt.get("title");
            Long price = Long.valueOf(reselt.get("price").toString());
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });

    }

    @Test
    public void testQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("id:1");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = (String) bean.getId();
            String title = (String) bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });
        //从响应数据中获取结果数据
//        SolrDocumentList results = queryResponse.getResults();
//        results.forEach(reselt->{
//            String id = (String) reselt.get("id");
//            String title = (String) reselt.get("title");
//            Long price = Long.valueOf(reselt.get("price").toString());
//            System.out.println("id:"+id+" title："+title+" price:"+price);
//        });
    }
    //组合查询
    //AND OR NOT
    @Test
    public void testBooleanQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("title:手机 AND price:[400000 TO 600000] NOT title:金立");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = (String) bean.getId();
            String title = (String) bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });
    }
    //排序
    @Test
    public void testSortQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //给查询条件指定排序条件
        solrQuery.setSort("price",SolrQuery.ORDER.desc);

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = (String) bean.getId();
            String title = (String) bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });
    }
    //分页
    @Test
    public void testPageQueryIndexWithBean() throws SolrServerException {

        int currentPage = 1;
        final int PAGE_SIZE = 5;
        int start = (currentPage-1)*PAGE_SIZE;

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //给查询条件指定排序条件
        solrQuery.setStart(start);
        solrQuery.setRows(PAGE_SIZE);

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = (String) bean.getId();
            String title = (String) bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });
    }
    @Test
    public void testLikeQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象,模糊查询本质就是≈，要模糊就需要打开模糊的开关
        //如果要修改最大编辑距离只需要在约等于模糊开关后跟对应的数字就可以了
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("title:vcva~2");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = (String) bean.getId();
            String title = (String) bean.getTitle();
            Long price = bean.getPrice();
            System.out.println("id:"+id+" title："+title+" price:"+price);
        });
    }
    @Test
    public void testHighLightQueryIndexWithBean() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
        //创建查询条件对象,模糊查询本质就是≈，要模糊就需要打开模糊的开关
        //如果要修改最大编辑距离只需要在约等于模糊开关后跟对应的数字就可以了
//        SolrQuery solrQuery = new SolrQuery("title:手机");
        SolrQuery solrQuery = new SolrQuery("title:手机");
        //指定高亮查询
        solrQuery.addHighlightField("title");
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

//        双层map第一次key为高亮文档的id,第二层map的key为高亮字段的名称，值为高亮的结果，
//        由于schema对字段的值可以多值，所以api设计为集合
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
        beans.forEach(bean->{
            String id = bean.getId();
            String highLightTitle = highlighting.get(id).get("title").get(0);
            Long price=bean.getPrice();
            System.out.println("id:"+id+" title："+highLightTitle+" price:"+price);

        });

        //从响应数据中获取结果数据
//        List<Heima56> beans = queryResponse.getBeans(Heima56.class);
//        beans.forEach(bean->{
//            String id = (String) bean.getId();
//            String title = (String) bean.getTitle();
//            Long price = bean.getPrice();
//            System.out.println("id:"+id+" title："+title+" price:"+price);
//        });
    }

}
